/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.workingtimemanager.urb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import de.workingtimemanager.urb.db.DatabaseHandler;
import de.workingtimemanager.urb.entities.WorkDay;

/**
 *
 * @author CK05102
 */
public class ExamineDayActivity extends Activity {

    private Context context;
    private DatabaseHandler db;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here
        db = new DatabaseHandler();
        setContentView(R.layout.examineday);
        context = this;
        createView();
    }

    private void createView() {
        final TextView date = (TextView) findViewById(R.id.examineday_date_text);
        final TextView startTime = (TextView) findViewById(R.id.examineday_starttime_text);
        final TextView endTime = (TextView) findViewById(R.id.examineday_endtime_text);
        final TextView duration = (TextView) findViewById(R.id.examineday_duration_text);
        Button save = (Button) findViewById(R.id.examineday_save_button);

        try {
            db.open(context);
            final WorkDay activeWorkDay = db.getActiveUser().getActiveProject().getActiveWorkDay();
            if (activeWorkDay != null) {
                date.setText(activeWorkDay.getDATE());
                startTime.setText(activeWorkDay.getSTARTTIME());
                endTime.setText(activeWorkDay.getENDTIME());
                duration.setText(activeWorkDay.getDURATION());
            }
            save.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    //TODO: reset active workday
                    WorkDay perhapsSavedDay = new WorkDay(date.getText().toString(), startTime.getText().toString(), endTime.getText().toString(), duration.getText().toString());
                    try {
                        db.open(context);
                        WorkDay savedDay = db.getActiveUser().getActiveProject().getActiveWorkDay();
                        if (!perhapsSavedDay.equals(savedDay)) {
                            savedDay.setDATE(perhapsSavedDay.getDATE());
                            savedDay.setSTARTTIME(perhapsSavedDay.getSTARTTIME());
                            savedDay.setENDTIME(perhapsSavedDay.getENDTIME());
                            savedDay.setDURATION(perhapsSavedDay.getDURATION());
                            db.insert(savedDay);
                        }
                    } finally {
                        db.close();
                        startActivity(new Intent(context, ViewTimersActivity.class));
                    }
                }
            });

        } finally {
            db.close();
        }
    }
}
